gtkprintcontext: Fix several potential g_object_[un]ref(NULL) calls
authorPhilip Withnall <philip.withnall@collabora.co.uk>
Tue, 26 Nov 2013 15:04:45 +0000 (15:04 +0000)
committerPhilip Withnall <philip.withnall@collabora.co.uk>
Mon, 9 Mar 2015 13:41:37 +0000 (13:41 +0000)
The page_setup of a GtkPrintContext or GtkPrintUnixDialog is nullable,
so all reference count changes to it have to be guarded against NULL
values.

Found by scan-build.

https://bugzilla.gnome.org/show_bug.cgi?id=712760

gtk/gtkprintcontext.c
gtk/gtkprintunixdialog.c

index f65de539ccf64dde5706243aff10a10584146181..d3c5a026352df9c1ea824e3c60991e0a84f08483 100644 (file)
@@ -355,8 +355,9 @@ _gtk_print_context_set_page_setup (GtkPrintContext *context,
   g_return_if_fail (GTK_IS_PRINT_CONTEXT (context));
   g_return_if_fail (page_setup == NULL ||
                    GTK_IS_PAGE_SETUP (page_setup));
-  
-  g_object_ref (page_setup);
+
+  if (page_setup != NULL)
+    g_object_ref (page_setup);
 
   if (context->page_setup != NULL)
     g_object_unref (context->page_setup);
index e1c19bc6e9ce147ac9923a5f4a1530a12b834b43..85fca0d3321ca3c47205d99378003c1f53041588 100644 (file)
@@ -2092,8 +2092,8 @@ selected_printer_changed (GtkTreeSelection   *selection,
           if (page_setup && priv->page_setup)
             gtk_page_setup_set_orientation (page_setup, gtk_page_setup_get_orientation (priv->page_setup));
 
-          g_object_unref (priv->page_setup);
-          priv->page_setup = page_setup;
+          g_clear_object (&priv->page_setup);
+          priv->page_setup = page_setup; /* transfer ownership */
         }
 
       priv->printer_capabilities = gtk_printer_get_capabilities (printer);
@@ -3249,7 +3249,7 @@ custom_paper_dialog_response_cb (GtkDialog *custom_paper_dialog,
                              gtk_paper_size_get_display_name (gtk_page_setup_get_paper_size (priv->page_setup))) == 0)
                 gtk_print_unix_dialog_set_page_setup (print_dialog, page_setup);
 
-              g_object_unref (page_setup);
+              g_clear_object (&page_setup);
             } while (gtk_tree_model_iter_next (model, &iter));
         }
     }
@@ -3437,7 +3437,7 @@ gtk_print_unix_dialog_set_page_setup (GtkPrintUnixDialog *dialog,
 
   if (priv->page_setup != page_setup)
     {
-      g_object_unref (priv->page_setup);
+      g_clear_object (&priv->page_setup);
       priv->page_setup = g_object_ref (page_setup);
 
       priv->page_setup_set = TRUE;